# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None
    
    def __hash__(self):
        return id(self)
    
    def __eq__(self, other):
        return self is other
    
    def __ne__(self, other):
        return self is not other

class Solution(object):
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        nodeSet = set()
        while head:
            if head not in nodeSet:
                nodeSet.add(head)
            else:
                return True
            head = head.next
        return False